<template>
    <!--审核课题（指定人）-->
    <div class="review-topic">
        <InnerBreadcrumb v-if="!idTip" v-model="indexKey" :path="breadPath" :showBack="true" />
        <div class="edu-page-warp" v-show="indexKey == 1">
            <!--批量驳回 弹框-->
            <edu-dialog
                title="批量驳回"
                width="45%"
                @close="onRejectClose"
                :visible="rejectDialogVisible"
            >
                <el-form
                    :model="rejectForm"
                    :rules="rejectRules"
                    ref="rejectForm"
                    label-position="right"
                    label-width="100px"
                    label-suffix="："
                >
                    <el-form-item label="审核意见" prop="remark">
                        <el-input
                            v-model="rejectForm.remark"
                            type="textarea"
                            :rows="4"
                            maxlength="100"
                            class="search-item"
                            show-word-limit
                            placeholder="请输入"
                        ></el-input>
                    </el-form-item>
                </el-form>
                <div slot="buttons">
                    <el-button @click="onRejectClose">取消</el-button>
                    <el-button type="danger" @click="onRejectSubmit">驳回</el-button>
                </div>
            </edu-dialog>

            <condition-card @reset="reset" @search="searchList" label-width="140px">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="毕业年份">
                            <el-date-picker
                                class="search-item"
                                :clearable="false"
                                v-model="queryForm.grade"
                                @change="handleGradeChange"
                                value-format="yyyy"
                                type="year"
                                placeholder="请选择"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="课题所属学院">
                            <el-select
                                v-model="queryForm.college"
                                filterable
                                @change="handleCollegeChange"
                                class="search-item"
                                clearable
                            >
                                <el-option
                                    v-for="item in source.collegeOptions"
                                    :key="item.collegeId"
                                    :label="item.collegeIdI18n"
                                    :value="item.collegeId"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="课题所属专业">
                            <el-select
                                v-model="queryForm.subjectId"
                                filterable
                                class="search-item"
                                clearable
                            >
                                <el-option
                                    v-for="item in source.subjectOptions"
                                    :key="item.subjectId"
                                    :label="item.subjectName"
                                    :value="item.subjectId"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="课题类型">
                            <el-select
                                v-model="queryForm.projectType"
                                filterable
                                class="search-item"
                                clearable
                            >
                                <el-option
                                    v-for="item in source.subjectTypeOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="课题性质">
                            <el-select
                                v-model="queryForm.projectNatureId"
                                filterable
                                class="search-item"
                                clearable
                            >
                                <el-option
                                    v-for="item in source.subjectNatureOptions"
                                    :key="item.id"
                                    :label="item.subjectProperties"
                                    :value="item.id"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="课题来源">
                            <el-select
                                v-model="queryForm.projectSourceId"
                                filterable
                                class="search-item"
                                clearable
                            >
                                <el-option
                                    v-for="item in source.subjectSourceOptions"
                                    :key="item.id"
                                    :label="item.subjectSource"
                                    :value="item.id"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="导师">
                            <el-input
                                v-model="queryForm.teacherNum"
                                class="search-item"
                                placeholder="请输入"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审核状态">
                            <el-select
                                v-model="queryForm.status"
                                filterable
                                class="search-item"
                                clearable
                            >
                                <el-option
                                    v-for="item in source.statusOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="课题名称">
                            <el-input
                                v-model="queryForm.projectName"
                                class="search-item"
                                placeholder="请输入"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </condition-card>
            <el-card shadow="never">
                <div class="edu-page-title">审核课题</div>
                <div class="edu-opertion-bar">
                    <div class="float-right">
                        <el-button
                            @click="onExport"
                            v-loading.fullscreen.lock="loading"
                            element-loading-text="Loading"
                            element-loading-spinner="el-icon-loading"
                            element-loading-background="rgba(255, 255, 255, .48)"
                        >导出</el-button>
                        <el-button type="danger" @click="onBatchReject">驳回</el-button>
                        <el-button type="primary" @click="onBatchPass">通过</el-button>
                    </div>
                </div>

                <edu-table
                    ref="reviewSubjectTable"
                    :url="getList"
                    @selection-change="handleSelectChange"
                >
                    <edu-table-column label="毕业年份" prop="grade" min-width="90"></edu-table-column>
                    <edu-table-column label="课题名称" prop="projectName" min-width="100"></edu-table-column>
                    <edu-table-column label="导师" prop="teacherName" min-width="100"></edu-table-column>
                    <edu-table-column label="课题所属学院" prop="faculty" min-width="120"></edu-table-column>
                    <edu-table-column label="课题所属专业" prop="subject" min-width="120"></edu-table-column>
                    <edu-table-column label="课题类型" prop="projectType" min-width="90"></edu-table-column>
                    <edu-table-column label="课题来源" prop="subjectSource" min-width="90"></edu-table-column>
                    <edu-table-column label="课题性质" prop="subjectProperties" min-width="90"></edu-table-column>
                    <edu-table-column label="审核状态" min-width="120">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.status === 'WaitCommit'" type="info">草稿</el-tag>
                            <el-tag v-else-if="scope.row.status === 'Process'" type="info">待审核</el-tag>
                            <el-tag v-else-if="scope.row.status === 'Reject'" type="danger">已驳回</el-tag>
                            <el-tag v-else-if="scope.row.status === 'End'" type="success">已通过</el-tag>
                        </template>
                    </edu-table-column>
                    <edu-table-column label="操作" min-width="100">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                @click="onReview(scope.row)"
                                v-if="scope.row.status === 'Process'"
                            >审核</el-button>
                            <el-button
                                type="text"
                                v-if="scope.row.status !== 'Process'"
                                @click="onView(scope.row)"
                            >查看</el-button>
                        </template>
                    </edu-table-column>
                </edu-table>
            </el-card>
        </div>

        <div v-if="indexKey === '2' || indexKey === '3'">
            <!--审核/查看-->
            <TopicStuDetail
                :actionType="currentAction"
                :basicInfoForm="basicInfoForm"
                :reviewForm= "reviewForm"
                :progressData="progressData"
                @topic-reject="topicReject"
                @topic-pass="topicPass"
                @topic-cancel="topicCancel"
            ></TopicStuDetail>
        </div>
        <div v-if="indexKey === '4' || indexKey === '5'">
            <!--审核/查看-->
            <TopicTeacherDetail
                :actionType="currentAction"
                :basicInfoForm="basicInfoForm"
                :progressData="progressData"
                :reviewForm= "reviewForm"
                @topic-reject="topicReject"
                @topic-pass="topicPass"
                @topic-cancel="topicCancel"
            ></TopicTeacherDetail>
        </div>
    </div>
</template>
<script>
import API from 'common/src/api/underGraduationDesign/reviewSubject'
import { auditData, batchData, exportData, detail } from 'common/src/api/underGraduationDesign/changeAudit'
import { approvelDetail } from 'common/src/api/approval'
import mixin from 'common/src/mixins/index'
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
    name: 'ChangeAudit',
    props: {
      dataInfo: {
        type: Object,
        default: {}
      },
      idTip:{
        type: String,
        default:""
      }
    },
    mixins: [mixin],
    data() {
        return {
            api: API,
            getList: '/api/designservice/pro/change/audit/list',
            queryForm: {
                grade: '',
                college: '',
                subjectId: '',
                authId:''
            },
            loading: false,
            indexKey: this.$route.query.indexKey?this.$route.query.indexKey:1,
            source: {
                collegeOptions: [], // 学院
                subjectOptions: [], // 专业
                // 课题类型
                subjectTypeOptions: [
                    {
                        label: '毕业论文',
                        value: '毕业论文'
                    },
                    {
                        label: '毕业设计',
                        value: '毕业设计'
                    }
                ],
                subjectNatureOptions: [], // 课题性质
                subjectSourceOptions: [], // 课题来源
                // 审核状态
                statusOptions: [
                    {
                        label: '草稿',
                        value: 'WaitCommit'
                    },
                    {
                        label: '待审核',
                        value: 'Process'
                    },
                    {
                        label: '已驳回',
                        value: 'Reject'
                    },
                    {
                        label: '已通过',
                        value: 'End'
                    }
                ]
            },
            breadPath: {
                label: '审核课题变更',
                key: '1',
                children: [
                    {
                        key: '2',
                        label: '审核'
                    },
                    {
                        key: '3',
                        label: '查看'
                    },
                    {
                        key: '4',
                        label: '审核'
                    },
                    {
                        key: '5',
                        label: '查看'
                    }
                ]
            },
            subjectSelectedData: [], // 勾选的列表数据
            dialogVisible: false,
            currentAction: '',
            // 批量驳回
            rejectDialogVisible: false,
            rejectForm: {},
            reviewForm: {},
            rejectRules: {
                remark: [{ required: true, message: '请输入', trigger: 'blur' }]
            },
            basicInfoForm: {
              applicationStudentEntity:{
                projectName:''
              }
            },
            progressData: [] //审核进度列表
        }
    },
    created() {
        this.queryForm.authId = localStorage.activeAuthId;
        this.queryForm.grade = new Date().getFullYear() + '';
        this.getCollege()
        this.getSubjectSource()
        this.getSubjectNature()
        if(this.dataInfo&&this.dataInfo.no){
          this.basicInfoForm=this.dataInfo.content;
          this.basicInfoForm.flowNo=this.dataInfo.no;
          this.currentAction = this.$route.query.currentAction;
          var applicationStudentEntity={
            projectName:this.basicInfoForm.oldProjectName,
            subtitleName:this.basicInfoForm.oldSubtitleName,
            projectSourceId:this.basicInfoForm.oldProjectSourceId,
            projectNatureId:this.basicInfoForm.oldProjectNatureId,
            projectType:this.basicInfoForm.oldProjectType,
            designPlace:this.basicInfoForm.oldDesignPlace,
            practiceSocialId:this.basicInfoForm.oldPracticeSocialId,
            researchDirection:this.basicInfoForm.oldResearchDirection,
            introductionProject:this.basicInfoForm.oledIntroductionProject,
            teacherNum:this.basicInfoForm.oldTeacherNum
          }
          var applicationStudentHis={
            projectName:this.basicInfoForm.projectName,
            subtitleName:this.basicInfoForm.subtitleName,
            projectSourceId:this.basicInfoForm.projectSourceId,
            projectNatureId:this.basicInfoForm.projectNatureId,
            projectType:this.basicInfoForm.projectType,
            designPlace:this.basicInfoForm.designPlace,
            practiceSocialId:this.basicInfoForm.practiceSocialId,
            researchDirection:this.basicInfoForm.researchDirection,
            introductionProject:this.basicInfoForm.introductionProject,
            teacherNum:this.basicInfoForm.teacherNum
          }
          this.basicInfoForm.applicationStudentEntity=applicationStudentEntity;
          this.basicInfoForm.applicationStudentHis=applicationStudentHis;
          this.progressData=this.dataInfo.listApproveHistory;
        }else{
          this.searchList()
        }
    },
    mounted() {
        this.searchList()
    },
    methods: {
        searchList() {
            this.$refs.reviewSubjectTable.search(this.queryForm)
        },
        reset() {
            this.queryForm = {
                grade: '',
                college: '',
                subjectId: '',
                authId: localStorage.activeAuthId
            }
          this.queryForm.grade = new Date().getFullYear() + '';
          this.getSubjectSource()
          this.getSubjectNature()
            this.searchList()
        },
        handleSelectChange(val) {
            this.subjectSelectedData = val
        },
        handleGradeChange(val) {
            if (val && this.queryForm.college) {
                this.handleCollegeChange(this.queryForm.college)
            }
          this.queryForm.grade = val;
          this.getSubjectSource()
          this.getSubjectNature()
          this.searchList()
        },
        // 查学院
        getCollege() {
            this.api.getCollege().then(res => {
                this.source.collegeOptions = res
            })
        },
        // 选择毕业年份和学院查专业
        handleCollegeChange(val) {
            this.queryForm.subjectId = ''
            this.source.subjectOptions = []
            if (val) {
                const params = {
                    grade: this.queryForm.grade,
                    college: val
                }
                this.api
                    .getSubject(params)
                    .then(res => {
                        this.source.subjectOptions = res.data
                    })
                    .catch()
            }
        },
        // 查课题来源
        getSubjectSource() {
            const params = {
                year: this.queryForm.grade
            }
            this.api
                .getSource(params)
                .then(res => {
                    this.source.subjectSourceOptions = res.data
                })
                .catch()
        },
        // 查课题性质
        getSubjectNature() {
            const params = {
                year: this.queryForm.grade
            }
            this.api
                .getNature(params)
                .then(res => {
                    this.source.subjectNatureOptions = res.data
                })
                .catch()
        },
        onDesignated() {
            this.dialogVisible = true
        },
        // 审核
        onReview(row) {
            this.currentAction = 'REVIEW_STU'
            if (row.applicationType == 2) {
                this.currentAction = 'REVIEW_TUTOR'
            }
            // this.reviewForm.remark = "";
            detail(row.id, row.applicationType).then(res => {
                if (row.applicationType == 1) {
                    this.basicInfoForm = res.data
                } else {
                    this.basicInfoForm = res.data
                }
            })
            approvelDetail({"no": row.flowNo}).then(res => {
                if (res && res.data) {
                    this.progressData = res.data.listApproveHistory
                }
            })
            if (row.applicationType == 1) {
                this.indexKey = '2'
            } else {
                this.indexKey = '4'
            }
        },
        // 审核驳回
        topicReject(data) {
            auditData(
                data
            ).then(res => {
                if (res.code == 200) {
                    this.$message({
                            type: 'success',
                            message: "驳回成功！"
                        })
                        if(this.dataInfo&&this.dataInfo.no){

                        }else{
                          this.searchList()
                        }

                    this.indexKey = 1
                } else {
                    this.$message({
                            type: 'fail',
                            message: "驳回异常！"
                        })
                }
            })
        },
        // 审核通过
        topicPass(data) {
            auditData(
                data
            ).then(res => {
                if (res.code == 200) {
                    this.$message({
                            type: 'success',
                            message: "审核成功！"
                        })
                    if(this.dataInfo&&this.dataInfo.no){

                    }else{
                      this.searchList()
                    }
                    this.indexKey = 1
                } else {
                    this.$message({
                            type: 'fail',
                            message: "审核失败！"
                        })
                }
            })
        },
        // 审核取消
        topicCancel() {
            this.indexKey = 1
        },
        // 查看
        onView(row) {
            this.currentAction = 'VIEW_STU'
            if (row.applicationType == 2) {
                this.currentAction = 'VIEW_TUTOR'
            }
            detail(row.id, row.applicationType).then(res => {
                if (row.applicationType == 1) {
                    this.basicInfoForm = res.data
                } else {
                    this.basicInfoForm = res.data
                }
            })
            approvelDetail({"no": row.flowNo}).then(res => {
                if (res && res.data) {
                    this.progressData = res.data.listApproveHistory
                }
            })
            if (row.applicationType == 1) {
                this.indexKey = '3'
            } else {
                this.indexKey = '5'
            }
        },
        // 批量通过
        onBatchPass() {
            if (this.subjectSelectedData.length === 0) {
                this.$eduAlert('请选择数据', '提示')
                return false
            }
            for (var index = 0 ;index< this.subjectSelectedData.length; index++) {
                if (this.subjectSelectedData[index].status == "Reject" || this.subjectSelectedData[index].status == "End") {
                     this.$eduAlert('请选择待审核状态的数据', '提示')
                     return false
                }
            }
            this.$eduConfirm('您确定通过吗？', '提示').then(() => {
                var idList = []
                for (var index = 0; index < this.subjectSelectedData.length; index++) {
                    idList.push(this.subjectSelectedData[index].flowNo)
                }
                var data = {
                    idList: idList,
                    desc: "",
                    type: 1
                }
                console.info(data);
                batchData(data).then(res => {
                    if (res.code == 200) {
                        this.$message({
                            type: 'success',
                            message: "审核成功！"
                        })
                        this.searchList()
                    }
                })
            })
        },
        // 批量驳回
        onBatchReject() {
            if (this.subjectSelectedData.length === 0) {
                this.$eduAlert('请选择数据', '提示')
                return false
            }
            for (var index = 0 ;index< this.subjectSelectedData.length; index++) {
                if (this.subjectSelectedData[index].status == "Reject" || this.subjectSelectedData[index].status == "End") {
                     this.$eduAlert('请选择待审核状态的数据', '提示')
                     return false
                }
            }
            this.rejectForm = {}
            this.rejectDialogVisible = true
            this.$nextTick(() => {
                this.$refs.rejectForm.clearValidate()
            })
        },
        onRejectClose() {
            this.rejectDialogVisible = false
            this.searchList()
        },
        // 批量驳回提交
        onRejectSubmit() {
            this.$refs.rejectForm.validate(valid => {
                if (valid) {
                    var idList = []
                    for (var index = 0; index < this.subjectSelectedData.length; index++) {
                        idList.push(this.subjectSelectedData[index].flowNo)
                    }
                    var data = {
                        idList: idList,
                        desc: this.rejectForm.remark,
                        type: 2
                    }
                    batchData(data).then(res => {
                        if (res.code == 200) {
                            this.$message({
                            type: 'success',
                            message: "驳回成功！"
                        })
                            this.onRejectClose()
                        }
                    })
                } else {
                    return false
                }
            })
        },
        // 导出
        onExport() {
            var params = {
                condition: this.queryForm,
                list: this.subjectSelectedData
            }
            exportData(params).then(res => {
                if (res.code == 200) {
                    // this.delayRequest(res.data.key)
                  tools.downloadFile(res.data.key, "审核课题变更.xls", null, this);
                }
            })
        },
        // 查文件导出状态，等待导出
        async delayRequest(key) {
            this.loading = true
            await new Promise(resolve => setTimeout(resolve, 3000))
            this.api
                .getExportStatus(key)
                .then(res => {
                    this.loading = false
                    if (res.code === 200 && res.data.status) {
                        let path = 'api/designservice/common/export?path=' + res.data.path
                        window.open(path)
                    } else {
                        this.delayRequest(key)
                    }
                })
                .catch(err => {
                    this.loading = false
                    this.delayRequest(key)
                })
        }
    },
    components: {
        TopicTeacherDetail: () => import('../topicsChange/components/topicTeacherDetail'),
        TopicStuDetail: () => import('../topicsChange/components/topicStuDetail')
    }
}
</script>
<style lang="scss" scoped>
.review-topic {
    .search-item {
        width: 100%;
    }
}
</style>
